/**

This file is part of MaCI/GIMnet.

MaCI/GIMnet is free software: you can redistribute it and/or modify it 
under the terms of the GNU Lesser General Public License as published 
by the Free Software Foundation, either version 3 of the License, or 
(at your option) any later version.

MaCI/GIMnet is distributed in the hope that it will be useful, but WITHOUT 
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 
FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public 
License for more details.

You should have received a copy of the GNU Lesser General Public 
License along with GIMnet. (See COPYING.LESSER) If not, see 
<http://www.gnu.org/licenses/>.

**/
/**
 * This example demonstrates how to receive data from another GIMI-client.
 * 
 * Check other files for more advanced tutorials on GIMI-features.
 */

#include "gimi.h"

int main(int argc, char **argv) {
  
  // Initialize debug printing
  debugInit();
  debugSetGlobalDebugLvl(6);
  
  int messageCount = 0;
  
  // Create the GIMI-object.
  gimi::GIMI gi;
  
  // Connect to tcphub on ASRobo
  if (!gi.connectToHub("asrobo.hut.fi", 50002, "SimpleReceiver")) {
    dPrint(1, "Could not connect to tcpHub. Are connection parameters correct?");
    return -1;
  } else {
    dPrint(1, "Successfully connected.");
  }
  
  // Message-object for receiving. See gimimessage.h for description.
  gimi::GIMIMessage message;
  
  // Receiver loop that ends after receiving 2 messages.
  while (messageCount < 2) {
    
    // Calling receive with infinite timeout. It won't return until a message is received
    // (or connection to hub is lost)
    // This receive gets messages with all protocoltypes.
    int receiveResult = gi.receive(message, -1);
    
    // Check if receiving succeeded.
    if (receiveResult == GIMI_OK) {
      dPrint(1, "Received a message!");
      printf("Message has majortype: %d minortype: %d msgid: %d\n"
             "It is from client: 0x%08x with flags: %d\n"
             "It arrived: %ld.%06ld s\n", message.getMajorTypeId(), message.getMinorTypeId(),
             message.getMessageId(), message.getSenderId(), message.getMessageFlags(),
             message.arrivalTime.seconds, message.arrivalTime.useconds);
      
      // Check if message is of type that we can understand.
      if (message.getMajorTypeId() == GIMI_PROTOCOL_IMAGESERVER_CTRL) { 
        printf("Message is a ImageServer Control -packet.\n");
        printf("It has %d bytes of data.\n", message.getDatalength());
        
        // Here we typecast the data within the message to the header for easier handling.
        TGIMI_PROTOCOL_IMAGESERVER_CTRL* packetContent =
            reinterpret_cast< TGIMI_PROTOCOL_IMAGESERVER_CTRL* >(message.getData());
        
        // Now we can access parameters within data via the header.
        printf("ImageServer Control -packet shutter parameter is set to %d\n", packetContent->shutter);
        
      } else {
        printf("Message was of unknown type\n");
      }
      
      // Increase message counter by one.
      ++messageCount;
    } else {
      // Print the received error code and it's description
      dPrint(1, "Error %d (%s) occurred, quitting.", receiveResult, gimi::getGimiErrorString(receiveResult).c_str());
    }
  }
  
  // Closes logfile in case if it was used. (debug printing)
  debugDeinit();
  
  return 0;
}
